package com.niuwa.p2p.service.customer.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.niuwa.p2p.core.mvc.entity.PageBean;
import com.niuwa.p2p.core.utils.AppConstants;
import com.niuwa.p2p.core.utils.DictConstant;
import com.niuwa.p2p.core.utils.StringUtil;
import com.niuwa.p2p.dao.account.AccountDao;
import com.niuwa.p2p.dao.customer.CustomerAuditDao;
import com.niuwa.p2p.dao.customer.CustomerDao;
import com.niuwa.p2p.dao.dictionary.DictionaryDao;
import com.niuwa.p2p.entity.Dictionary;
import com.niuwa.p2p.entity.PageQuery;
import com.niuwa.p2p.entity.account.Account;
import com.niuwa.p2p.entity.customer.CustLoginLog;
import com.niuwa.p2p.entity.customer.CustPwdReset;
import com.niuwa.p2p.entity.customer.Customer;
import com.niuwa.p2p.entity.customer.CustomerAudit;
import com.niuwa.p2p.exception.BusinessException;
import com.niuwa.p2p.service.common.ICommonService;
import com.niuwa.p2p.service.customer.ICustomerService;
import com.niuwa.p2p.vo.customer.BaseCustomerInfoVo;
import com.niuwa.p2p.vo.customer.BaseInvestVo;
import com.niuwa.p2p.vo.customer.BaseLoanVo;
import com.niuwa.p2p.vo.customer.CustomerAuditVo;
import com.niuwa.p2p.vo.customer.CustomerVo;
import com.niuwa.p2p.vo.front.account.CustomerInformation;

@Service
public class CustomerServiceImpl implements ICustomerService {
	
	@Autowired
	private CustomerDao customerDao;	
	
	@Autowired
	private CustomerAuditDao auditDao;
	
	@Autowired
	private AccountDao accountDao;
	
	@Autowired
	private CustomerAuditDao customerAuditDao;
	
	@Autowired
	private ICommonService commonService;
	
	@Autowired
	private DictionaryDao dictionaryDao;

	// 账户操作状态: 锁定
	private final String FROZEN = DictConstant.AccountOperStatus.LOCK.getCode();
	// 账户操作状态: 解锁
	private final String UNFROZEN = DictConstant.AccountOperStatus.UNLOCK.getCode();
	// 账户操作状态: 禁用
	private final String DISABLE = DictConstant.AccountOperStatus.DISABLED.getCode();
	// 账户操作状态: 启用
	private final String ENBALE = DictConstant.AccountOperStatus.ENABLE.getCode();
	// 账户操作状态: 注销
	private final String WRITEOFF = DictConstant.AccountOperStatus.CANCEL.getCode();
	//客户状态审核
	private final String CUST_AUDIT = DictConstant.AuditType.CUST_AUDIT.getCode();
	//资金账户状态审核
	private final String ACCOUNT_AUDIT = DictConstant.AuditType.ACCOUNT_AUDIT.getCode();
	//账户审核状态-等待审核
	private final String WAITING_AUDIT = DictConstant.AuditStatus.WAITING_AUDIT.getCode();
	//账户审核状态-审核通过
	private final String AUDIT_PASSED = DictConstant.AuditStatus.AUDIT_PASSED.getCode();
	//账户审核状态-审核拒绝
	private final String AUDIT_REJECT = DictConstant.AuditStatus.AUDIT_REJECT.getCode();
	//客户状态-注销
	private final String CUST_CANCEL = DictConstant.CustStatus.CANCEL.getCode();
	//资金账户状态-注销
	private final String ACCOUNT_CANCEL = DictConstant.AccountStatus.CANCEL.getCode();
		
	/**
	 * 分页查询客户集合
	 * 根据paramMap，选取需要的客户集合 
	 * 
	 * @param paramMap 过滤得条件Map
	 * @return PageBean<CustomerVo> 分页信息与分页数据集合（客户集合） 
	 */
	@Override
	public PageBean<CustomerVo> search(Map<String, Object> paramMap) {

		assert paramMap.get(PageQuery.PAGEQUERY) != null;

		int totalCount = customerDao.count(paramMap);

		PageBean<CustomerVo> page = new PageBean<CustomerVo>(totalCount);

		// 当前页
		page.setPage(((PageQuery) paramMap.get(PageQuery.PAGEQUERY)).getPage());

		page.setRows(customerDao.selectCustomerBySelective(paramMap));

		return page;
	}

	/**
	 * 锁定客户 
	 * 锁定对象:状态为正常的客户
	 * 限制优先级如下:
	 * 注销>禁用>锁定>正常
	 * 
	 * @param custId 客户ID
	 * @return 更新条数
	 * @throws 锁定失败(更新DB失败)
	 */
	@Override
	public int updateCustomerToFroze(long custId, long applyUserId) throws Exception {
		int result = updateCustomerStatus(custId, FROZEN, applyUserId);
		if(result != 1) {
			throw new BusinessException("customerService.failTOForze");
		}
		return result;	
	}

	/**
	 * 解锁客户 
	 * 解锁对象:状态为锁定的客户
	 * 只有当客户为锁定状态,才能解锁.
	 * 解锁后,客户状态变更为正常.
	 * 
	 * @param custId 客户ID
	 * @return 更新条数
	 * @throws 解锁失败(更新DB失败)
	 */
	@Override
	public int updateCustomerToUnfroze(long custId, long applyUserId) throws Exception {
		int result = updateCustomerStatus(custId, UNFROZEN, applyUserId);
		if(result != 1) {
			throw new BusinessException("customerService.failTOUnforze");
		}
		return result;
	}

	/**
	 * 禁用客户 
	 * 禁用对象:状态为正常,锁定的客户
	 * 限制优先级如下:
	 * 注销>禁用>锁定>正常
	 * 
	 * @param custId 客户ID
	 * @return 更新条数
	 * @throws 禁用失败(更新DB失败)
	 */
	@Override
	public int updateCustomerToDisable(long custId, long applyUserId) throws Exception {
		int result = updateCustomerStatus(custId, DISABLE, applyUserId);
		if(result != 1) {
			throw new BusinessException("customerService.failTODisable");
		}
		return result;
	}

	/**
	 * 启用客户
	 * 启用对象:状态为禁用的客户
	 * 只有当客户为禁用状态,才能启用.
	 * 启用后,客户状态变更为正常.
	 * 
	 * @param custId 客户ID
	 * @return 更新条数
	 * @throws 锁定失败(更新DB失败)
	 */
	@Override
	public int updateCustomerToEnable(long custId, long applyUserId) throws Exception {
		int result = updateCustomerStatus(custId, ENBALE, applyUserId);
		if(result != 1) {
			throw new BusinessException("customerService.failTOEnable");
		}
		return result;
	}

	/**
	 * 注销客户 
	 * 注销对象:状态为正常,锁定,禁用的客户
	 * 限制优先级如下:
	 * 注销>禁用>锁定>正常
	 * 
	 * @param custId 客户ID
	 * @return 更新条数
	 * @throws 注销失败(更新DB失败)
	 */
	@Override
	public int updateCustomerToWriteOff(long custId, String writeOffReasonCust, String uploadedFileIdCust, long applyUserId) throws Exception {
				
		int result = 0;
		// 取得客户信息
		Customer customer = customerDao.selectByCustId(custId);
		// 不存在,抛出错误
		if(customer == null) {
			throw new BusinessException("customerService.custIsNotExist");
		}
		Account account = accountDao.selectByCustId(customer.getId());
		// 不存在,抛出错误
		if(account == null) {
			throw new BusinessException("customerService.accountIsNotExist");
		}
		// 检查权限
		if(hasAuthToUpdateCustomerStatus(customer.getCustStatus(), WRITEOFF, account) == true) {
			CustomerAudit customerAudit = new CustomerAudit();
			customerAudit.setCustId(customer.getId());
			customerAudit.setAuditAction(DictConstant.CustStatus.CANCEL.getCode());
			customerAudit.setAuditType(DictConstant.AuditType.CUST_AUDIT.getCode());
			customerAudit.setAuditStatus(DictConstant.AuditStatus.WAITING_AUDIT.getCode());
			customerAudit.setBeforeStatus(customer.getCustStatus());				
			customerAudit.setApplyUserId(applyUserId);
			customerAudit.setReason(writeOffReasonCust);							
			customerAudit.setAttachmentId(uploadedFileIdCust);
			// 更新状态
			result = customerAuditDao.insertCustomerAudit(customerAudit);
		} else {
			throw new BusinessException("customerService.canNotWriteOffForAccountIsNotZero");
		}
		if(result != 1) {
			throw new BusinessException("customerService.failTOWriteOff");
		}
		return result;
	}
	
	/**
	 * 更新客户状态
	 * 
	 * @param custId 客户ID
	 * @param nextStatus 下个状态
	 * @return 更新条数
	 * @throws 客户不存在
	 */
	private int updateCustomerStatus(long custId, String nextStatus, long applyUserId) throws Exception {
		int result = 0;
		// 取得客户信息
		Customer customer = customerDao.selectByCustId(custId);
		// 不存在,抛出错误
		if(customer == null) {
			throw new BusinessException("customerService.custIsNotExist");
		}
		Account account = accountDao.selectByCustId(customer.getId());
		// 不存在,抛出错误
		if(account == null) {
			throw new BusinessException("customerService.accountIsNotExist");
		}
		// 检查权限
		if(hasAuthToUpdateCustomerStatus(customer.getCustStatus(), nextStatus, account) == true) {
			if(FROZEN.equals(nextStatus)) {
				customer.setCustStatus(nextStatus);
				result = customerDao.updateByCustSelective(customer);
			} else if(UNFROZEN.equals(nextStatus)) {
				customer.setCustStatus(DictConstant.CustStatus.NORMAL.getCode());
				result = customerDao.updateByCustSelective(customer);
			} else {
				CustomerAudit customerAudit = new CustomerAudit();
				customerAudit.setCustId(customer.getId());
				customerAudit.setAuditType(DictConstant.AuditType.CUST_AUDIT.getCode());
				customerAudit.setAuditStatus(DictConstant.AuditStatus.WAITING_AUDIT.getCode());
				customerAudit.setBeforeStatus(customer.getCustStatus());	
				customerAudit.setApplyUserId(applyUserId);
				// 设置下个状态
				customerAudit.setAuditAction(nextStatus);
				// 更新状态
				result = customerAuditDao.insertCustomerAudit(customerAudit);
			}
		}
		// 无权限直接返回
		return result;
	}
	
	/**
	 * 检查状态权限
	 * 
	 * @param preStatus 上个状态
	 * @param nextStatus 下个状态
	 * 
	 * @return true:有权限  false:没有权限
	 */
	private boolean hasAuthToUpdateCustomerStatus(String preStatus, String nextStatus, Account account) {
		
		// 下个状态为:解锁  前状态:不为锁定的时候,没有权限
		if(UNFROZEN.equals(nextStatus) && !DictConstant.CustStatus.LOCK.getCode().equals(preStatus)) {
			return false;
		}
		// 下个状态为:启用  前状态:不为禁用的时候,没有权限		
		if(ENBALE.equals(nextStatus) && !DictConstant.CustStatus.DISABLED.getCode().equals(preStatus)) {
			return false;
		}
		int inextStatus = Integer.parseInt(nextStatus);
		int ipreStatus = Integer.parseInt(preStatus);
		// 下个状态<前状态,没有权限
		if(inextStatus <= ipreStatus) {
			return false;
		}
		// 下个状态:注销
		if(nextStatus.equals(WRITEOFF)) {
			// 返回是否有注销权限
			return hasAuthToWriteOff(account);
		}
		return true;
	}
	
	/**
	 * 锁定账户 
	 * 锁定对象:状态为正常的账户
	 * 限制优先级如下:
	 * 注销>禁用>锁定>正常
	 * 
	 * @param custId 客户ID
	 * @return 更新条数
	 * @throws 锁定失败(更新DB失败)
	 */
	@Override
	public int updateToAccountForze(long custId,  long applyUserId) throws Exception {
		int result = updateAccountStatus(custId, FROZEN, applyUserId);
		if(result != 1) {
			throw new BusinessException("customerService.failTOForze");
		}
		return result;
	}

	/**
	 * 解锁账户 
	 * 解锁对象:状态为锁定的账户
	 * 只有当账户为锁定状态,才能解锁.
	 * 解锁后,账户状态变更为正常.
	 * 
	 * @param custId 客户ID
	 * @return 更新条数
	 * @throws 解锁失败(更新DB失败)
	 */
	@Override
	public int updateAccountToUnfroze(long custId,  long applyUserId) throws Exception {
		int result = updateAccountStatus(custId, UNFROZEN, applyUserId);
		if(result != 1) {
			throw new BusinessException("customerService.failTOUnforze");
		}
		return result;
	}

	/**
	 * 禁用账户 
	 * 禁用对象:状态为正常,锁定的账户
	 * 限制优先级如下:
	 * 注销>禁用>锁定>正常
	 * 
	 * @param custId 客户ID
	 * @return 更新条数
	 * @throws 禁用失败(更新DB失败)
	 */
	@Override
	public int updateAccountToDisable(long custId, long applyUserId) throws Exception {
		int result = updateAccountStatus(custId, DISABLE, applyUserId);
		if(result != 1) {
			throw new BusinessException("customerService.failTODisable");
		}
		return result;
	}

	/**
	 * 启用账户
	 * 启用对象:状态为禁用的账户
	 * 只有当账户为禁用状态,才能启用.
	 * 启用后,账户状态变更为正常.
	 * 
	 * @param custId 客户ID
	 * @return 更新条数
	 * @throws 锁定失败(更新DB失败)
	 */
	@Override
	public int updateAccountToEnable(long custId, long applyUserId) throws Exception {
		int result = updateAccountStatus(custId, ENBALE, applyUserId);
		if(result != 1) {
			throw new BusinessException("customerService.failTOEnable");
		}
		return result;
	}

	/**
	 * 注销账户 
	 * 注销对象:状态为正常,锁定,禁用的账户
	 * 限制优先级如下:
	 * 注销>禁用>锁定>正常
	 * 
	 * @param custId 客户ID
	 * @return 更新条数
	 * @throws 注销失败(更新DB失败)
	 */
	@Override
	public int updateAccountToWriteOff(long custId, String writeOffReasonAccount, String uploadedFileIdAccount, long applyUserId) throws Exception {
		
		int result = 0;
		// 取得客户信息
		Customer customer = customerDao.selectByCustId(custId);
		// 不存在,抛出错误
		if(customer == null) {
			throw new BusinessException("customerService.custIsNotExist");
		}
		Account account = accountDao.selectByCustId(customer.getId());
		// 不存在,抛出错误
		if(account == null) {
			throw new BusinessException("customerService.accountIsNotExist");
		}
		// 检查权限
		if(hasAuthToUpdateAccountStatus(account.getAccountStatus(), WRITEOFF, account) == true) {
			CustomerAudit customerAudit = new CustomerAudit();
			customerAudit.setCustId(customer.getId());
			customerAudit.setAuditAction(DictConstant.AccountStatus.CANCEL.getCode());
			customerAudit.setAuditType(DictConstant.AuditType.ACCOUNT_AUDIT.getCode());
			customerAudit.setAuditStatus(DictConstant.AuditStatus.WAITING_AUDIT.getCode());
			customerAudit.setBeforeStatus(account.getAccountStatus());
			customerAudit.setApplyUserId(applyUserId);
			customerAudit.setReason(writeOffReasonAccount);
			customerAudit.setAttachmentId(uploadedFileIdAccount);	
			// 更新状态
			result = customerAuditDao.insertCustomerAudit(customerAudit);
		} else {
			throw new BusinessException("customerService.canNotWriteOffForAccountIsNotZero");
		}
		if(result != 1) {
			throw new BusinessException("customerService.failTOWriteOff");
		}
		return result;
	}
	
	
	/**
	 * 更新账户状态
	 * 
	 * @param custId 客户ID
	 * @param nextStatus 下个状态
	 * @return 更新条数
	 * @throws 账户不存在
	 */
	private int updateAccountStatus(long custId, String nextStatus, long applyUserId) throws Exception {
		int result = 0;
		// 取得客户信息
		Customer customer = customerDao.selectByCustId(custId);
		// 不存在,抛出错误
		if(customer == null) {
			throw new BusinessException("customerService.custIsNotExist");
		}
		Account account = accountDao.selectByCustId(customer.getId());
		// 不存在,抛出错误
		if(account == null) {
			throw new BusinessException("customerService.accountIsNotExist");
		}
		// 检查权限
		if(hasAuthToUpdateAccountStatus(account.getAccountStatus(), nextStatus, account) == true) {
			if(FROZEN.equals(nextStatus)) {
				account.setAccountStatus(nextStatus);
				result = accountDao.updateByAccountSelective(account);
			} else if (UNFROZEN.equals(nextStatus)) {
				account.setAccountStatus(DictConstant.AccountStatus.NORMAL.getCode());
				result = accountDao.updateByAccountSelective(account);
			} else {
				CustomerAudit customerAudit = new CustomerAudit();
				customerAudit.setCustId(customer.getId());
				customerAudit.setAuditType(DictConstant.AuditType.ACCOUNT_AUDIT.getCode());
				customerAudit.setAuditStatus(DictConstant.AuditStatus.WAITING_AUDIT.getCode());
				customerAudit.setBeforeStatus(account.getAccountStatus());
				customerAudit.setApplyUserId(applyUserId);
				// 设置下个状态
				customerAudit.setAuditAction(nextStatus);
				// 更新状态
				result = customerAuditDao.insertCustomerAudit(customerAudit);
			}
		}
		// 无权限直接返回
		return result;
	}
	
	/**
	 * 检查状态权限
	 * 
	 * @param preStatus 上个状态
	 * @param nextStatus 下个状态
	 * 
	 * @return true:有权限  false:没有权限
	 */
	private boolean hasAuthToUpdateAccountStatus(String preStatus, String nextStatus, Account account) {
		
		// 下个状态为:解锁  前状态:不为锁定的时候,没有权限
		if(UNFROZEN.equals(nextStatus) && !DictConstant.AccountStatus.LOCK.getCode().equals(preStatus)) {
			return false;
		}
		// 下个状态为:启用  前状态:不为禁用的时候,没有权限		
		if(ENBALE.equals(nextStatus) && !DictConstant.AccountStatus.DISABLED.getCode().equals(preStatus)) {
			return false;
		}
		int inextStatus = Integer.parseInt(nextStatus);
		int ipreStatus = Integer.parseInt(preStatus);
		// 下个状态<前状态,没有权限
		if(inextStatus <= ipreStatus) {
			return false;
		}
		// 下个状态:注销
		if(nextStatus.equals(WRITEOFF)) {
			// 返回是否有注销权限
			return hasAuthToWriteOff(account);
		}
		return true;
	}

	/**
	 * 检查注销权限
	 * 
	 * @param account 账户
	 * @return true:有权限  false:没有权限
	 */
	private boolean hasAuthToWriteOff(Account account){
		// 资金账户（含货币基金账户）余额为零且无待回款项目
		if(account.getAcctBal().compareTo(BigDecimal.ZERO) == 0
				&& account.getForzenAmount().compareTo(BigDecimal.ZERO) == 0
				&& account.getCollectingPrincipal().compareTo(BigDecimal.ZERO)  == 0
				&& account.getCollectingRevenue().compareTo(BigDecimal.ZERO) == 0) {
			return true;
		}
		return false;
	}
	
	@Override
	public PageBean<BaseInvestVo> selectInvest(Map<String, Object> paramMap) {
		assert paramMap.get(PageQuery.PAGEQUERY) != null;
		//判断输入是姓名or电话号码
		if(!StringUtil.isEmpty((String) paramMap.get("nameOrTel"))){	
			paramMap.put((String) paramMap.get("nameOrTel"), paramMap.get("nameOrTel"));
		}
		//总条数
		int totalCount = customerDao.selectInvestCountBySelective(paramMap);
		PageBean<BaseInvestVo> page = new PageBean<BaseInvestVo>(totalCount);
		// 当前页
		page.setPage(((PageQuery) paramMap.get(PageQuery.PAGEQUERY))
				.getPage());
		page.setRows(customerDao.selectInvestBySelective(paramMap));
		return page;
	}
	
	@Override
	public PageBean<BaseLoanVo> selectLoanList(Map<String, Object> paramMap) {
		assert paramMap.get(PageQuery.PAGEQUERY) != null;
		//判断输入是姓名or电话号码
		if(!StringUtil.isEmpty((String) paramMap.get("nameOrTel"))){	
			paramMap.put((String) paramMap.get("nameOrTel"), paramMap.get("nameOrTel"));
		}
		//总条数
		int totalCount = customerDao.selectLoanCountBySelective(paramMap);
		PageBean<BaseLoanVo> page = new PageBean<BaseLoanVo>(totalCount);
		// 当前页
		page.setPage(((PageQuery) paramMap.get(PageQuery.PAGEQUERY))
				.getPage());
		page.setRows(customerDao.selectLoanBySelective(paramMap));
		return page;
	}

	@Override
	public PageBean<CustomerAuditVo> selectCustomerAuditList(Map<String, Object> paramMap) {
		assert paramMap.get(PageQuery.PAGEQUERY) != null;
		//总条数
		int totalCount = auditDao.selectCustomerAuditCountBySelective(paramMap);
		PageBean<CustomerAuditVo> page = new PageBean<CustomerAuditVo>(totalCount);
		// 当前页
		page.setPage(((PageQuery) paramMap.get(PageQuery.PAGEQUERY))
				.getPage());
		page.setRows(auditDao.selectCustomerAuditBySelective(paramMap));
		return page;
	}
	
	@Override
	public int updatePlatOrAccountStatusOff(CustomerAuditVo auditVo) throws Exception {
		int result = 0;
		if(auditVo != null){
			//注销状态(平台、资金)
			String auditType = auditVo.getAuditType();
			//客户ID
			long custId = auditVo.getCustId();
			// 取得客户信息
			Customer customer = customerDao.selectByCustId(custId);
			// 不存在,抛出错误
			if(customer == null) {
				throw new BusinessException("customerService.custIsNotExist");
			}else{
				if(!StringUtil.isEmpty(auditType)){
					if(CUST_AUDIT.equals(auditType)){//平台操作
						if(CUST_CANCEL.equals(auditVo.getAuditAction())){//若为注销，则平台和资金账户联动注销
							//资金状态
							if(ENBALE.equals(auditVo.getAuditAction())){//状态改为启用，即状态为正常
								auditVo.setAccountStatus(DictConstant.AccountStatus.NORMAL.getCode());
							}else{
								auditVo.setAccountStatus(auditVo.getAuditAction());
							}
							result = auditDao.updateAccountStatusById(auditVo);
							if(result > 0){
								//客户状态
								customer.setCustStatus(auditVo.getAuditAction());
								//客户ID
								customer.setId(auditVo.getCustId());
								result = customerDao.updateByCustSelective(customer);
								if(result > 0){
									//修改审核表审核信息
									result = updatePassAuditInfo(auditVo);
								}
							}
						}else{
							//若不是注销，只对请求进行处理
							//客户状态
							if(ENBALE.equals(auditVo.getAuditAction())){//状态改为启用，即状态为正常
								customer.setCustStatus(DictConstant.CustStatus.NORMAL.getCode());
							}else{
								customer.setCustStatus(auditVo.getAuditAction());
							}
							//客户ID
							customer.setId(auditVo.getCustId());
							result = customerDao.updateByCustSelective(customer);
							if(result > 0){
								//修改审核表审核信息
								result = updatePassAuditInfo(auditVo);
							}
						}
					}else if(ACCOUNT_AUDIT.equals(auditType)){//资金操作
						//资金状态
						if(ENBALE.equals(auditVo.getAuditAction())){//状态改为启用，即状态为正常
							auditVo.setAccountStatus(DictConstant.AccountStatus.NORMAL.getCode());
						}else{
							auditVo.setAccountStatus(auditVo.getAuditAction());
						}
						result = auditDao.updateAccountStatusById(auditVo);
						if(result > 0){
							//修改审核表审核信息
							result = updatePassAuditInfo(auditVo);
						}
					}
				}
			}
		}
		return result;
	}

	@Override
	public int updatePassAuditInfo(CustomerAuditVo auditVo) throws Exception {
		int result = 0;
		if(auditVo != null) {
			//审核人ID（当前登录者ID）
			//TODO 修改
			//auditVo.setAuditor(String.valueOf(SessionUtil.getSessionUser().getId()));
			auditVo.setAuditStatus(AUDIT_PASSED);
			result = auditDao.updateAuditInfoById(auditVo);
		}
		return result;
	}

	@Override
	public int updateRefusedAuditInfo(CustomerAuditVo auditVo) throws Exception {
		int result = 0;
		if(auditVo != null) {
			//审核人ID（当前登录者ID）
			//TODO 修改
			//auditVo.setAuditor(String.valueOf(SessionUtil.getSessionUser().getId()));
			auditVo.setAuditStatus(AUDIT_REJECT);
			result = auditDao.updateAuditInfoById(auditVo);
		}
		return result;
	}

	@Override
	public Customer getCustomerById(Long id) {
		return customerDao.selectByCustId(id);
	}
	
	@Override
	public PageBean<BaseCustomerInfoVo> searchRegUsersList(
			Map<String, Object> paramMap) {
		
		assert paramMap.get(PageQuery.PAGEQUERY) != null;
		
		int count = customerDao.seachRegUserListCount(paramMap);
		
		PageBean<BaseCustomerInfoVo> page = new PageBean<BaseCustomerInfoVo>(count);

		page.setPage(((PageQuery) paramMap.get(PageQuery.PAGEQUERY)).getPage());

		page.setRows(customerDao.seachRegUserList(paramMap));
		
		return page;
	}
	

	/**
	 * 根据用户信息相关字典【web】
	 * @return
	 */
	@Override
	public Map<String, List<Dictionary>> getPersonalInformationDict() throws Exception {
		Map<String, List<Dictionary>> map=new HashMap<String, List<Dictionary>>();
		//认证状态
		List<Dictionary> authDict=dictionaryDao.getDictByDictType(AppConstants.AUTH_STATUS);
		//获取性别
		List<Dictionary> sexDict=dictionaryDao.getDictByDictType(AppConstants.SEX);
		//婚姻状况
		List<Dictionary> marrDict=dictionaryDao.getDictByDictType(AppConstants.MARRIAGE);
		//学历
		List<Dictionary> educDict=dictionaryDao.getDictByDictType(AppConstants.EDUCATION);
		//生肖
		List<Dictionary> zodiacDict=dictionaryDao.getDictByDictType(AppConstants.ZODIAC);
		//星座
		List<Dictionary> constellationDict=dictionaryDao.getDictByDictType(AppConstants.CONSTELLATION);
		//交易类型
		List<Dictionary> transDict=dictionaryDao.getDictByDictType(AppConstants.TRANSACTION_TYPE);
		//是、否
		List<Dictionary> blackDict=dictionaryDao.getDictByDictType(AppConstants.BLACK_LIST_STATUS);
		//资金账户银行卡状态
		List<Dictionary> bcStatusDict=dictionaryDao.getDictByDictType(AppConstants.BIND_CARD_STATUS);
		//投资状态
		List<Dictionary> investRecordStatusDict = dictionaryDao.getDictByDictType(AppConstants.INVEST_RECORD_STATUS); 
		//投资方式
		List<Dictionary> investMethodDict = dictionaryDao.getDictByDictType(AppConstants.INVEST_METHOD);
		//还款状态
		List<Dictionary> repayStatusDict = dictionaryDao.getDictByDictType(AppConstants.REPAY_STATUS);
		//债权转让状态
		List<Dictionary> creditTransStatusDict = dictionaryDao.getDictByDictType(AppConstants.CREDIT_TRANS_STATUS);
		//密保问题
		List<Dictionary> safetyProblemDict=dictionaryDao.getDictByDictType(AppConstants.CUST_SAFETY_PROBLEM);
		//资金账户状态
		List<Dictionary> fundAccStatusDict=dictionaryDao.getDictByDictType(AppConstants.ACCOUNT_STATUS);
		//资金流水处理状态 
		List<Dictionary> flowDealStatusDict=dictionaryDao.getDictByDictType(AppConstants.FLOW_DEAL_STATUS);
		
		map.put("authDict", authDict);
		map.put("sexDict", sexDict);
		map.put("marrDict", marrDict);
		map.put("educDict", educDict);
		map.put("zodiacDict", zodiacDict);
		map.put("constellationDict", constellationDict);
		map.put("transDict", transDict);
		map.put("blackDict", blackDict);
		map.put("bcStatusDict", bcStatusDict);
		map.put("investRecordStatusDict", investRecordStatusDict);
		map.put("investMethodDict", investMethodDict);
		map.put("repayStatusDict", repayStatusDict);
		map.put("creditTransStatusDict", creditTransStatusDict);
		map.put("safetyProblemDict", safetyProblemDict);
		map.put("fundAccStatusDict", fundAccStatusDict);
		map.put("flowDealStatusDict", flowDealStatusDict);
		return map;
	}

	/**
	 * 获取用户认证信息【web】
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public CustomerInformation getCustomerAuth(long id) throws Exception {
		return customerDao.getCustomerAuth(id);
	}


	/**
	 * 修改密码【web】
	 * @param paramMap
	 * @return
	 */
	@Override
	public int updatePassWrod(Map<String, Object> paramMap) {
		return customerDao.updatePassWrod(paramMap);
	}

	/**
	 * 更新客户[web] 
	 * @param user 更新的客户 对象
	 * @throws Exception
	 */
	@Override
	public int updateByCustSelective(Customer customer) throws Exception {
		return customerDao.updateByCustSelective(customer);
	}
	
	
	/**
	 * 
	 * 查询登录列表
	 * @param paramMap
	 * @return page
	 */

	public PageBean<CustLoginLog> searchLoginInfoList(Map<String, Object> paramMap) {
		assert paramMap.get(PageQuery.PAGEQUERY) != null;
		
		int count = customerDao.searchLoginInfoListCount(paramMap);
		
		PageBean<CustLoginLog> page = new PageBean<CustLoginLog>(count);

		page.setPage(((PageQuery) paramMap.get(PageQuery.PAGEQUERY)).getPage());

		page.setRows(customerDao.searchLoginInfoList(paramMap));
		
		return page;

	}

	
	
	/**
	 * 
	 * 查询密码重置列表
	 * @param paramMap
	 * @return page
	 */
	@Override
	public PageBean<CustPwdReset> searchPasswordResetList(Map<String, Object> paramMap) {
      assert paramMap.get(PageQuery.PAGEQUERY) != null;
		
		int count = customerDao.searchPasswordResetListCount(paramMap);
		
		PageBean<CustPwdReset> page = new PageBean<CustPwdReset>(count);

		page.setPage(((PageQuery) paramMap.get(PageQuery.PAGEQUERY)).getPage());

		page.setRows(customerDao.searchPasswordResetList(paramMap));
		
		return page;
	}


	
	//====================== 前台使用  Add by bwz begin ==============================
	@Override
	public Customer getCustomerByLoginName(String loginName) throws Exception{
		Customer customer = customerDao.findByLoginName(loginName);
		return customer;
	}

	@Override
	public BaseInvestVo selectUserInvestInfo(long customerId) {
		BaseInvestVo investinfo = customerDao.selectUserInvestInfo(customerId);
		return investinfo;
	}

	//====================== 前台使用  Add by bwz end =================================
	
	/**
     * 更改用户密保问题[web]
     * @param record
     * @return
     */
	@Override
	public Customer getCustomerBySelective(Map<String, Object> paramMap) {
		return customerDao.getCustomerBySelective(paramMap);
	}

	@Override
	public Map<String, Object> findCustomerByIdAndMobile(Map<String, Object> paramMap) {
		return customerDao.findCustomerByIdAndMobile(paramMap);
	}

	@Override
	public Customer selectByEmail(String email) {
		return customerDao.selectByEmail(email);
	}

	
}
